package com.xingshang.config;

import com.xingshang.interceptor.LoginProtectedInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginProtectedInterceptor loginProtectedInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //排除user/wxlogin 接口
        registry.addInterceptor(loginProtectedInterceptor).addPathPatterns("/playlist/**")
                .addPathPatterns("/user/**")
                .addPathPatterns("/recent/**")
                .excludePathPatterns("/user/wxlogin")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/regist")
                .addPathPatterns("/likeSong/**")
        ;
    }

    @Value("${file.upload.dir}")
    private String uploadDir;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/**")  // 访问路径前缀
                .addResourceLocations("file:" + uploadDir + "/")  // 实际文件存储路径
                .setCachePeriod(3600)  // 缓存时间（秒）
                .resourceChain(true);  // 开启资源链

        System.out.println("uploadDir: " + uploadDir);  // 添加日志
    }
}
